package cn.cai.pethome.shop.controller;

import cn.cai.pethome.base.PreAuthorize;
import cn.cai.pethome.shop.dto.ShopDto;
import cn.cai.pethome.shop.mapper.ShopMapper;
import cn.cai.pethome.shop.service.IShopService;
import cn.cai.pethome.shop.domain.Shop;
import cn.cai.pethome.shop.query.ShopQuery;
import cn.cai.pethome.base.util.AjaxResult;
import cn.cai.pethome.base.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.jar.JarEntry;

@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    public IShopService shopService;


    /**
     * 批量导入店铺入驻信息
     * @param file
     * @return
     */
    @PostMapping("/importExecl")
    public AjaxResult importExecl(MultipartFile file){
        try {
            shopService.importExecl(file);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("Excel导入失败!!!" + e.getMessage());
        }
    }

    /**
     * 激活
     * @param id
     * @return
     */
    @GetMapping("/activate/{id}")
    public AjaxResult activated(@PathVariable Long id){
        try {

            shopService.updateStated(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
     * 平台员工：审核商家入驻信息
     * jackson转换的
     * @RequestBody: 读取请求体里面的json字符串，转化为形参变量类型指定的实体类对象
     * @ResponseBody: 将你controller方法返回的对象转化为json字符串，放入响应的消息体里面返回给前端
     * @param         //shopDt  前端的对象
     * @return
     */
    @PreAuthorize(name = "店铺入驻审核",sn = "shop:audit",parent_id = 1)
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody ShopDto shopDto){
        try {
            shopService.audit(shopDto);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("商家审核入驻信息失败！！！" + e.getMessage());
        }
    }


    /**
     * 商家入驻
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){

        try {
            shopService.settlement(shop);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("入驻表单提交失败！程序员已经在吐血！！！" + e.getMessage());
        }
    }


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping("/save")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null){
                shopService.update(shop);}
            else{

                shopService.add(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！" + e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error("删除对象失败！" + e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping("/list")
    public List<Shop> list(){
        return shopService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PreAuthorize(name = "店铺列表",sn = "shop:pagelist",menu_id = 10,parent_id = 1)
    @PostMapping("/pagelist")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.queryAll(query);
    }
}
